package reduce.fz.com.reduce.fragment.base;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbRecycleScroollListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseListAdapter;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.util.HttpUtils;

public abstract class BaseListFragment<VH extends BaseRecycleViewHolder, VC> extends Fragment {
    //Adapter
    protected BaseListAdapter<VH, VC> baseListAdapter;

    public List<VC> datas;
    //上下文
    protected Context context;
    //分页
    private  Pagination pagination = new Pagination();

    private SwipeRefreshLayout swipeRefreshLayout;

    protected  RecyclerView recyclerView;

    protected LayoutInflater layoutInflater;

    private LinearLayoutManager mLayoutManager;
    private int layoutRid;


    public BaseListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        layoutInflater = inflater;
        View view = layoutInflater.inflate(R.layout.fragment_baselist, null);
        super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.recyclerView = initRecycleView(view);
        this.swipeRefreshLayout = initSwipeRefresh(view);
        this.context = this.getActivity().getBaseContext();
        pagination.pageType=getPageType();
        initListAdapter();
        initRecycleView();
        this.recyclerView.setAdapter(this.baseListAdapter);
        firstPage();

    }

    private  RecyclerView initRecycleView(View view) {
        return (RecyclerView) view.findViewById(R.id.base_list_view);
    }

    private  SwipeRefreshLayout initSwipeRefresh(View view) {
        return (SwipeRefreshLayout) view.findViewById(R.id.base_swipe_refresh);
    }

    /**
     * 获取LayoutManager，兼容GridLayoutManager
     * @return
     */
    public abstract LinearLayoutManager getLayoutManager();
    /**
     * 创建item card
     * @param parent
     * @return
     */
    protected abstract VH createItemViewHolder(ViewGroup parent);

    /**
     * 创建foot
     * @param view
     * @return
     */
    protected abstract VH createfootViewHolder(View view);

    /**
     * 在请求之前获取url,如果为statNum类型的分页方式，需要讲startNUm重设到pagination中
     * @param datas
     * @param pagination
     * @return
     */

    protected abstract String setUrlBeforeRequest(List<VC> datas,Pagination pagination);

    //获取分页类型，默认pageNo
    protected abstract  int getPageType();

    public void initListAdapter() {
        this.baseListAdapter = new BaseListAdapter<VH, VC>(context) {
            @Override
            public VH createFooterStatusViewHolder(View footerView) {
                return createfootViewHolder(footerView);
            }

            @Override
            public VH createViewHolder(ViewGroup parent) {
                return createItemViewHolder(parent);
            }
        };
    }

    public String combUrl(Pagination pagination) {
        String url = setUrlBeforeRequest(datas, pagination);
        pagination.nextPage();
        if (pagination.pageType==Pagination.TYPE_START_NUM)
            url = url + "?startNum=" + pagination.getStartNum() + "&pageSize=" + pagination.getPageSize();
        else
            url = url + "?pageNo=" + pagination.getPageNo() + "&pageSize=" + pagination.getPageSize();
        return url;
    }

    //翻下一页
    public void nextPage() {
        pagination.nextPage();
        String url = combUrl(pagination);
        request(url);

    }

    //翻第一页
    public void firstPage() {
        pagination.reset();
        String url = combUrl(pagination);
        request(url);

    }

    //请求URL
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<VC>>(new TypeReference<Result<List<VC>>>() {
        }) {
            @Override
            public void responseInfo(Result<List<VC>> data) {
                if (!data.isSuccess()) return;
                baseListAdapter.setFooterNormal();
                baseListAdapter.addDatas(data.getData());
            }
        });
    }






    public void initRecycleView() {
        if (getLayoutManager() != null) mLayoutManager = getLayoutManager();
        else {
            mLayoutManager = new LinearLayoutManager(this.getActivity().getBaseContext());
        }
        // 设置ItemAnimator
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setOnScrollListener(new AbRecycleScroollListener(mLayoutManager) {
            @Override
            public void onLoadMore() {
                baseListAdapter.setFooterLoading();
                nextPage();
                baseListAdapter.setFooterNormal();

            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                firstPage();
                swipeRefreshLayout.setRefreshing(false);

            }
        });

    }


}
